﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TruckWatch.Cloud.Models.EF;
using AutoMapper;

namespace TruckWatch.Cloud.BusinessLogic
{
    public class MessageBL
    {
        public static void UpdateClientAck(string recipientUserId, int ackNumber)
        {
            TruckWatchEntities context = new TruckWatchEntities();

            Driver driver = DriverBLEF.GetDriverByUserId(recipientUserId, context, true);

            driver.Ack = ackNumber;
            driver.AckDate = DateTime.UtcNow;

            context.SaveChanges();
        }

        public static List<Message> GetAllMessagesSinceAck(string userId)
        {
            TruckWatchEntities context = new TruckWatchEntities();

            Driver driver = DriverBLEF.GetDriverByUserId(userId, context, true);

            int driverAck = driver.Ack.HasValue ? driver.Ack.Value : 0;

            return GetAllMessagesForUserId(userId).Where(m => m.Id > driverAck).ToList();

        }

        public static List<Message> GetAllMessagesForUserId(string userId)
        {
            TruckWatchEntities context = new TruckWatchEntities();

            Driver recipientDriver = DriverBLEF.GetDriverByUserId(userId);
            if (recipientDriver == null)
                return new List<Message>();

            return context.Messages.Where(m => m.RecipientDriverId == recipientDriver.Id || m.RecipientDriverId == GeneralSettings.BroadCastDriverId).OrderByDescending(x => x.SendDate).ToList();
        }

        public static void AddMessage(Message newMessage)
        {
            newMessage.SendDate = DateTime.UtcNow;

            TruckWatchEntities context = new TruckWatchEntities();

            context.AddToMessages(newMessage);
            context.SaveChanges();
        }

        public static List<TruckWatch.Cloud.Models.MessageModel> ConvertMessageToMessageModel(List<Message> messages)
        {
            //Mapper.CreateMap<Message, TruckWatch.Cloud.Models.MessageModel>();

            List<TruckWatch.Cloud.Models.MessageModel> messageModels = new List<TruckWatch.Cloud.Models.MessageModel>();
            foreach (Message message in messages)
            {
                TruckWatch.Cloud.Models.MessageModel messageModel = new TruckWatch.Cloud.Models.MessageModel();
                //AutoMapper.Mapper.Map<Message, TruckWatch.Cloud.Models.MessageModel>(message, messageModel);

                messageModel.Id = message.Id;
                messageModel.SenderDriverId = message.SenderDriverId;
                messageModel.SenderName = message.SenderDriver.Name;
                messageModel.RecipientDriverId = message.RecipientDriverId;
                messageModel.RecipientName = message.RecipientDriver.Name;
                messageModel.SendDate = message.SendDate;
                messageModel.ConversationId = message.ConversationId.HasValue ? message.ConversationId.Value : 0;
                messageModel.MessageText = message.MessageText;
                messageModel.MessageType = message.MessageType;
                messageModel.ParentMessageId = message.ParentMessageId;
                messageModel.ResponderDistance = message.ResponderDistance;
                messageModels.Add(messageModel);
            }

            return messageModels;
        }



        public static dynamic GetAllOutboundMessagesFromUserId(string userId)
        {
            TruckWatchEntities context = new TruckWatchEntities();

            Driver senderDriver = DriverBLEF.GetDriverByUserId(userId);
            if (senderDriver == null)
                return new List<Message>();

            return context.Messages.Where(m => m.SenderDriverId == senderDriver.Id).OrderByDescending(x => x.SendDate).ToList();
            
        }
    }
}
